package com.sloth.bear.controller;

import com.sloth.bear.dto.request.DocListRequest;
import com.sloth.bear.dto.request.DocumentMoveRequest;
import com.sloth.bear.dto.request.DocumentRequest;
import com.sloth.bear.dto.request.DocumentSortUpdateRequest;
import com.sloth.bear.dto.vo.DocumentDetailsVo;
import com.sloth.bear.dto.vo.DocumentListVo;
import com.sloth.bear.service.DocService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文档管理
 */
@RestController
@RequestMapping("/document")
@RequiredArgsConstructor
public class DocController {

    private final DocService docService;

    /**
     * 创建新文档
     */
    @PostMapping
    public DocumentListVo createDocument(@Valid @RequestBody DocumentRequest request) {
        return docService.createDocument(request);
    }

    /**
     * 获取文档列表
     */
    @GetMapping("/list")
    public List<DocumentListVo> listDoc(@Valid DocListRequest request) {
        return docService.listDoc(request);
    }
    
    /**
     * 根据ID获取文档详情
     */
    @GetMapping("/{id}")
    public DocumentDetailsVo getDocumentById(@PathVariable Long id) {
        return docService.getDocumentById(id);
    }

    /**
     * 更新文档信息
     */
    @PutMapping("/{id}")
    public DocumentListVo updateDocument(@PathVariable Long id, @Valid @RequestBody DocumentRequest request) {
        return docService.updateDocument(id, request);
    }

    /**
     * 删除文档
     */
    @DeleteMapping("/{id}")
    public boolean deleteDocument(@PathVariable Long id) {
        return docService.deleteDocument(id);
    }
    
    /**
     * 移动文档到新的父文档下
     */
    @PutMapping("/{id}/move")
    public boolean moveDocument(@PathVariable Long id, @Valid @RequestBody DocumentMoveRequest request) {
        return docService.moveDocument(id, request);
    }
    
    /**
     * 批量更新文档排序
     */
    @PostMapping("/batch/sort")
    public boolean batchUpdateSort(@Valid @RequestBody DocumentSortUpdateRequest request) {
        return docService.batchUpdateSort(request);
    }
    
    /**
     * 验证文档移动是否会造成循环引用
     */
    @GetMapping("/validate-move")
    public boolean validateMove(@RequestParam Long sourceId, @RequestParam Long targetId) {
        return docService.validateMove(sourceId, targetId);
    }
}